package com.ynrcc.efiles;

import java.util.List;

import com.eova.aop.MetaObjectIntercept;
import com.eova.common.Easy;
import com.eova.common.utils.xx;
import com.eova.model.Button;
import com.eova.model.Menu;
import com.eova.model.MetaField;
import com.eova.model.MetaObject;
import com.eova.model.User;
import com.eova.vo.MenuConfig;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.ynrcc.model.Attachment;
import com.ynrcc.model.Doc;
import com.ynrcc.model.Receive;
import com.ynrcc.model.Send;

public class BorrowController extends Controller {

	final Controller ctrl = this;

	/** 自定义拦截器 **/
	protected MetaObjectIntercept intercept = null;

	/** 异常信息 **/
	private String errorInfo = "";

	/** 当前操作的主对象 **/
	private final Record record = new Record();

	/** 弹出借阅信息窗口 **/
	public void add() throws Exception {
		int doc_id = this.getParaToInt(0);

		// 当前用户信息
		User user = getSessionAttr("user");
		String brchcd = user.getStr("brchcd");
		String login_id = user.getStr("login_id");

		// 档案信息
		Doc doc = Doc.dao.findById(doc_id);
		String title = doc.getStr("title");

		setAttr("doc_id", doc_id);
		setAttr("brchcd", brchcd);
		setAttr("login_id", login_id);
		setAttr("title", title);

		render("/efiles/borrow/add.html");
	}

	/** 同意借阅 **/
	public void agree() {
		int id = getParaToInt(0);

		try {
			// 状态更新为"同意借阅"
			Db.use(xx.DS_EOVA).update("update borrow set status = ? where id = ?", 2, id);
		} catch (Exception e) {
			e.printStackTrace();
			renderJson(new Easy("更新借阅状态失败！"));
		}
		renderJson(new Easy());
	}
	
	/** 拒绝借阅 **/
	public void deny() {
		int id = getParaToInt(0);

		try {
			// 状态更新为"拒绝借阅"
			Db.use(xx.DS_EOVA).update("update borrow set status = ? where id = ?", 3, id);
		} catch (Exception e) {
			e.printStackTrace();
			renderJson(new Easy("更新借阅状态失败！"));
		}
		renderJson(new Easy());
	}

	public void list() {

		String menuCode = this.getPara(0);

		// 获取元数据
		Menu menu = Menu.dao.findByCode(menuCode);
		MenuConfig config = menu.getConfig();
		String objectCode = config.getObjectCode();
		MetaObject object = MetaObject.dao.getByCode(objectCode);
		List<MetaField> fields = MetaField.dao.queryByObjectCode(objectCode);

		// 根据权限获取功能按钮
		User user = this.getSessionAttr("user");
		List<Button> btnList = Button.dao.queryByMenuCode(menuCode, user.getInt("rid"));

		// 是否需要显示快速查询
		setAttr("isQuery", MetaObject.dao.isExistQuery(objectCode));

		setAttr("menu", menu);
		setAttr("btnList", btnList);
		setAttr("object", object);
		setAttr("fields", fields);

		render("/efiles/send/list.html");
	}

	public void detail() throws Exception {
		int id = getParaToInt(0);
		Send send = Send.dao.findById(id);
		setAttr("send", send);
		render("/efiles/send/detail.html");
	}

	// 查看完整档案
	public void showall() throws Exception {
		int id = getParaToInt(0);
		Doc doc = Doc.dao.findById(id);
		setAttr("doc", doc);

		// 相关的发文处理单
		String sql = "select * from Send where file_id =" + id;
		List<Send> send = Send.dao.find(sql);
		if (send.size() > 0) {
			setAttr("send", send);
		}

		// 相关的收文处理单
		sql = "select * from Receive where file_id =" + id;
		List<Receive> receive = Receive.dao.find(sql);
		if (receive.size() > 0) {
			setAttr("receive", receive);
		}

		// 相关的附件
		sql = "select * from Attachment where file_id =" + id;
		List<Attachment> attachment = Attachment.dao.find(sql);
		if (attachment.size() > 0) {
			setAttr("file_id", id);
		}

		render("/efiles/doc/showall.html");

	}

	/**
	 * 查找Dialog
	 */
	public void toFind() {
		render("/efiles/send/find/find.html");
	}

}
